<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
         <a-col :md="6" :sm="12">
            <a-form-item label="导师编号">
              <a-input v-model="queryParam.dsbh"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select allowClear v-model="queryParam.ssyxm" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
            <a-col :md="6" :sm="12">
                <a-form-item label="职称">
                  <a-select allowClear v-model="queryParam.zcm" showSearch :filterOption="filterOption">
                    <a-select-option v-for="i in TYPE0342" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
            </a-col>
          </template>
          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="showCheck">批量审核</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :scroll="{ x: 1400 }"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
      >
        <!-- :rowSelection="options.rowSelection" -->
        <!-- :alert="options.alert"
        -->
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="csrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <!-- <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a> -->
        <span slot="shbs" slot-scope="a">{{+a ? '新导师' : '老导师'}}</span>
        <template slot="operation" slot-scope="a,b">
          <a @click="direct(b)">审核</a>
        </template>
      </s-table>
    </a-card>

    <check-modal
      ref="checkModal"
      :visible.sync="visible"
      :pageType="'dsxx'"
      @close="() => { visible = false }"
      @save="batchCheck"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import checkModal from './component/checkModal'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '导师编号',
          dataIndex: 'dsbh',
          scopedSlots: { customRender: 'dsbh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '身份证号',
          dataIndex: 'sfzh'
        },
        {
          title: '所属院系',
          dataIndex: 'dsssyxmc'
        },
        {
          title: '导师职称',
          dataIndex: 'zcmc'
        },
        // {
        //   title: '审核标识',
        //   dataIndex: 'shbs',
        //   scopedSlots: { customRender: 'shbs' }
        // },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width:80,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.overseas.teacherCheckList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      confirmLoading: false,
      selectMenu: {},
      visible: false,
      initDone: false
    }
  },
  components: {STable, checkModal},
  computed: {
    ...mapState({
      TYPE0342: state => state.app['TYPE0342'], // 导师类别
      TYPE0320: state => state.app['TYPE0320'], // 导师状态
      TYPE0321: state => state.app['TYPE0321'], // 是否在岗
      TYPE0133: state => state.app['TYPE0133'] // 招生学年
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if ((from.name === '导师信息审核页') && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      }
      vm.initDone = true
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.name === '导师信息审核页') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0342')
    this.$store.dispatch('app/setDictionary', 'TYPE0320')
    this.$store.dispatch('app/setDictionary', 'TYPE0321')
    this.$store.dispatch('app/setDictionary', 'TYPE0133')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    direct (data) {
      this.$router.push(`/overseas/info/${data.dsbh}/check`)
    },
    showCheck () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要审核的导师')
        return
      }
      this.visible = true
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    batchCheck (params) {
      this.$refs.checkModal.confirmLoading = true
      let dsbhs = this.selectedRows.map(ds => {
        return ds.dsbh
      })
      this.$api.overseas.teacherCheck(Object.assign({dsbhs: dsbhs.join(',')}, params)).then(() => {
        this.$message.success('操作成功')
        this.selectedRowKeys = []
        this.selectedRows = []
        this.visible = false
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
        this.$refs.checkModal.form.resetFields()
      }).finally(() => {
        this.$refs.checkModal.confirmLoading = false
      })
    }
  }
}
</script>

<style>

</style>
